define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree', 'fileinput', 'vue'], function ($, undefined, Backend, Table, Form, undefined, fileinput, Vue) {
//读取选中的条目
    function initVue() {
        var vue = new Vue({
            el: '#form_div',
            data: {
                category1_list: [],
                category2_list: [],
                category3_list: [],
                category4_list: [],
                category5_list: [],
                category6_list: [],
                category7_list: [],
                category_id1: 0,
                category_id2: 0,
                category_id3: 0,
                category_id4: 0,
                category_id5: 0,
                category_id6: 0,
                category_id7: 0,
                current_level: 0,
                name: '',
                english_name: '',
            },
            watch: {
                category_id1: function (val) {
                    this.category_id1 = val;
                    this.category2_list = [];
                    this.category3_list = [];
                    this.category4_list = [];
                    this.category5_list = [];
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 1);
                },
                category_id2: function (val) {
                    this.category_id2 = val;
                    this.category3_list = [];
                    this.category4_list = [];
                    this.category5_list = [];
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 2);
                },
                category_id3: function (val) {
                    this.category_id3 = val;
                    this.category4_list = [];
                    this.category5_list = [];
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 3);
                },
                category_id4: function (val) {
                    this.category_id4 = val;
                    this.category5_list = [];
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 4);
                },
                category_id5: function (val) {
                    this.category_id5 = val;
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 5);
                },
                category_id6: function (val) {
                    this.category_id6 = val;
                    this.category7_list = [];
                    this.initSubCategoryList(val, 6);
                }
            },
            methods: {
                initCategoryList: function () {
                    this.initSubCategoryList(0, 0);
                },
                initSubCategoryList: function (parent_id, parent_level) {
                    var data = {'parent_id': parent_id};
                    var that = this;
                    $.get('adminpublic/ajax_category_list', data, function (res) {
                        switch (parent_level) {
                            case 0:
                                that.category1_list = res;
                                break;
                            case 1:
                                that.category2_list = res;
                                break;
                            case 2:
                                that.category3_list = res;
                                break;
                            case 3:
                                that.category4_list = res;
                                break;
                            case 4:
                                that.category5_list = res;
                                break;
                            case 5:
                                that.category6_list = res;
                                break;
                            case 6:
                                that.category7_list = res;
                                break;
                        }
                    });
                }
            }
        });
        return vue;
    }

    function initImageFileInput(ctrlName, uploadUrl) {
        $('#upload_image_file').fileinput('refresh', {
            language: 'zh', //设置语言
            uploadUrl: uploadUrl, //上传的地址
            allowedFileExtensions: ['xls', 'xlsx'],//接收的文件后缀
            showUpload: true, //是否显示上传按钮
            showRemove: false,
            showCaption: false,//是否显示标题
            browseClass: "btn btn-primary", //按钮样式
            initialPreviewConfig: [],
            previewFileIcon: "<i  class='glyphicon glyphicon-king'></i>",
        }).on("fileuploaded", function (event, data) {
            if (data.response.code == 1) {
                Toastr.success(data.response.msg);
                $('#' + ctrlName).val(data.response.data.url);
            } else {
                Toastr.error(data.response.msg);
            }
        }).on("filesuccessremove", function (event, data, previewId, index) {
            return false;
        });
    }

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'repository/category',
                    edit_url: 'repository/category/edit',
                    add_url: 'repository/category/add',
                    del_url: 'repository/category/del'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search: false,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'english_name', title: __('English'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {
                            field: 'current_level',
                            title: __('Level'),
                            formatter: function (value, row, index) {
                                value = value + '';
                                switch (value) {
                                    case '0':
                                        return __('Kingdom');
                                    case '1':
                                        return __('Phylum');
                                    case '2':
                                        return __('Class');
                                    case '3':
                                        return __('Order');
                                    case '4':
                                        return __('Family');
                                    case '5':
                                        return __('Genus');
                                    case '6':
                                        return __('Species');
                                }
                            },
                            searchList: {
                                '0': __('Kingdom'),
                                '1': __('Phylum'),
                                '2': __('Class'),
                                '3': __('Order'),
                                '4': __('Family'),
                                '5': __('Genus'),
                                '6': __('Species')
                            },
                            style: 'min-width:100px;'
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.buttons,
                            buttons: [
                                {
                                    name: 'upload_lower_level',
                                    text: __('Upload'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'repository/category/upload_lower_level'
                                },
                            ],
                            formatter: function (value, row, index) {
                                if (row.current_level == '6') {
                                } else {
                                    return Table.api.formatter.buttons.call(this, value, row, index);
                                }

                            }
                        },
                        {
                            field: 'operate1', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            var url = Config.site.cdnurl + '/index.php/admin/repository/category/import';
            // initFileInput(url);
            $(document).on("click", ".btn-import", function () {
                $('#uploadModal').modal();

            });
            $(document).on("click", "#upload_btn", function () {
                var form = new FormData(document.getElementById("upload_form"));
                $.ajax({
                    url: Config.site.cdnurl + '/index.php/admin/repository/category/import',
                    type: "POST",
                    data: form,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        if (data.code == 1) {
                            Toastr.success(data.msg);
                            $('#uploadModal').modal('hide');
                        } else {
                            Toastr.error(data.msg);
                        }
                    },
                    error: function (e) {
                    }
                });
            });
        },
        upload_lower_level: function () {
            Form.api.bindevent($("form[role=form]"));
            var category_id = Config.category_id;
            $(document).on("click", "#upload_lower_level_btn", function () {
                var form = new FormData(document.getElementById("upload_lower_level_form"));
                $.ajax({
                    url: Config.site.cdnurl + '/index.php/admin/repository/category/upload_lower_level' + '/ids/' + category_id,
                    type: "POST",
                    data: form,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        if (data.code == 1) {
                            Toastr.success(data.msg);
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);
                            window.parent.location.reload();
                        } else {
                            Toastr.error(data.msg);
                        }
                    },
                    error: function (e) {
                    }
                });
            });
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = initVue();
            var category = Config.category;
            console.info(category);
            vue.current_level = category.current_level;
            vue.name = category.name;
            vue.english_name = category.english_name;
            for (; category.current_level > 0; category.current_level--) {
                var level = category.current_level + '';
                switch (level) {
                    case '1':
                        vue.category_id1 = category.category_id1;
                        break;
                    case '2':
                        vue.category_id2 = category.category_id2;
                        break;
                    case '3':
                        vue.category_id3 = category.category_id3;
                        break;
                    case '4':
                        vue.category_id4 = category.category_id4;
                        break;
                    case '5':
                        vue.category_id5 = category.category_id5;
                        break;
                    case '6':
                        vue.category_id6 = category.category_id6;
                        break;
                }
            }
            console.info(vue.category_id1);
            vue.initCategoryList();
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = initVue();
            vue.initCategoryList();
        }
    };
    return Controller;
});